/*******************************************************************************
 * Copyright 2007-2014 FZI, http://www.fzi.de
 *                 Forschungszentrum Informatik - Information Process Engineering (IPE)
 *  
 *                 See the NOTICE file distributed with this work for additional
 *                 information regarding copyright ownership
 *                
 *                 Licensed under the Apache License, Version 2.0 (the "License");
 *                 you may not use this file except in compliance with the License.
 *                 You may obtain a copy of the License at
 *                
 *                   http://www.apache.org/licenses/LICENSE-2.0
 *                
 *                 Unless required by applicable law or agreed to in writing, software
 *                 distributed under the License is distributed on an "AS IS" BASIS,
 *                 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *                 See the License for the specific language governing permissions and
 *                 limitations under the License.
 * @author tzentek - <a href="mailto:zentek@fzi.de">Tom Zentek</a>
 * @author cyumusak - <a href="mailto:canyumusak@gmail.com">Can Yumusak</a>
 ******************************************************************************/
package de.fzi.fhemapi.view.vaadin.ui;

import java.net.ConnectException;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;

import de.fzi.fhemapi.server.FHEMServer;

/**
 * The inital panel which prompts the user to enter the ip and the port of the server.
 * This does not store those in cookies yet, because it is only supported by vaadin 7.
 * @author Can Yumusak
 *
 */
public class ServerDetailsForm extends CustomComponent{
    private static final String COMMON_FIELD_WIDTH = "12em";


    public ServerDetailsForm() {
    	FormLayout	layout = new FormLayout();
    	setCompositionRoot(layout);
    	
    	Label title = new Label("<h1> Server Details</h1>");
    	title.setContentMode(Label.CONTENT_XHTML);
    	layout.addComponent(title);
    	
    	final TextField serverIP = new TextField("Server IP");

    	serverIP.setWidth(COMMON_FIELD_WIDTH);
    	layout.addComponent(serverIP);
    		
    	final TextField port = new TextField("Port");
    	port.setWidth(COMMON_FIELD_WIDTH);
    	layout.addComponent(port);
    	
    	Button applyButton = new Button("Apply");
    	layout.addComponent(applyButton);
    	
    	applyButton.addListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				try {
//					Window mainWindow = new Window();
					FHEMServer server = new FHEMServer((String)serverIP.getValue(), Integer.parseInt((String) port.getValue()));
					getWindow().setContent(new HWindow(server));
//					VaadinServletResponse response = 
//							   (VaadinServletResponse) VaadinService.getCurrentResponse();
//							Cookie ipCookie = new Cookie("ip", (String)serverIP.getValue());
//							Cookie portCookie = new Cookie("port", (String) port.getValue());
//							ipCookie.setMaxAge(30 * 24 * 60 * 60);
//							ipCookie.setPath("/HWC");
//							portCookie.setMaxAge(30 * 24 * 60 * 60);
//							portCookie.setPath("/HWC");
//							response.addCookie(ipCookie);
//							response.addCookie(portCookie);
				} catch (ConnectException e) {
					getWindow().setContent(new Window("Could not connect to FHEM!\n Cause: "+e.getMessage()));
					getWindow().showNotification("Error!", e.getMessage(), Window.Notification.TYPE_ERROR_MESSAGE);
			}
			}
		});
//
//    	VaadinServletRequest request = 
//    			   (VaadinServletRequest) VaadinService.getCurrentRequest();
//    	if(request != null){
//    			Cookie[] cookies = request.getCookies();
//    			for (Cookie cookie : cookies) {
//    			   if (cookie.getName().equals("ip")) {
//    			      serverIP.setValue(cookie.getValue());
//    			   }else if(cookie.getName().equals("port")){
//    				   port.setValue(cookie.getValue());
//    			   }
//    			}
//    	}
    			
    }
}
